<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryForm" :inline="true">
        <!-- StartDate  EndDate -->
        <el-form-item prop="keywords">
          <el-input v-model="queryForm.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="20">
      <el-col :span="24" :offset="0">
        <!--右边数据-->
        <div class="table-container">
          <el-row :gutter="20">
            <el-col :span="24" :offset="0">
              <!-- <el-button v-if="permissions.create" type="primary" size="default" @click="Create">+新增</el-button> -->
              <!-- <el-button v-if="permissions.transferManager" type="primary" size="default" @click="TransferManager"
                >转入管理人员</el-button
              > -->
            </el-col>
            <el-col :span="24" :offset="0" style="margin-top: 10px">
              <el-table
                ref="tableRef"
                v-loading="loading"
                :data="tableData"
                style="width: 100%"
                :show-overflow-tooltip="{ effect: 'light' }"
                @select="handleCurrentChange"
              >
                <el-table-column type="selection" align="center" width="50" />
                <el-table-column label="项目名称" prop="projectName" min-width="150" />
                <el-table-column label="项目编号" prop="projectNum" min-width="170" />
                <el-table-column label="人员状态" prop="statusName" min-width="80" />
                <el-table-column label="姓名" prop="name" min-width="100" />
                <el-table-column label="所在班组" prop="groupName" min-width="120" />
                <el-table-column label="工种" prop="workTypeName" min-width="120" />
                <el-table-column label="联系电话" prop="mobile" min-width="120" />
                <el-table-column label="民族" prop="nation" min-width="80" />
                <!-- <el-table-column label="工资计算方式" prop="sizeTypeName" min-width="120" /> -->
                <el-table-column label="合同类型" prop="contractType" min-width="80" />
                <el-table-column label="合同签订日期" prop="signingDate" min-width="120" />
                <el-table-column label="工资支付日期" prop="salaryPaymentDate" min-width="120" />
                <el-table-column label="进场日期" prop="startDate" min-width="120" />
                <el-table-column label="操作" fixed="right" max-width="300" min-width="100">
                  <template #default="scope">
                    <!-- <el-button v-if="permissions.update" link size="small" type="primary" @click="Edit(scope.row)">
                      修改
                    </el-button>
                    <el-button
                      v-if="permissions.transferManager"
                      link
                      size="small"
                      type="primary"
                      @click="TransferManager(scope.row)"
                    >
                      转入管理组
                    </el-button> -->
                    <el-button
                      v-if="permissions.detail"
                      type="primary"
                      link
                      size="small"
                      @click="handleDetail(scope.row)"
                    >
                      详情
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <pagination
                v-if="total > 0"
                v-model:total="total"
                v-model:page="queryForm.pageIndex"
                v-model:limit="queryForm.pageSize"
                @pagination="handleQuery"
              />
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-row>

    <add-form
      :dialog-add-or-edit="dialogAddOrEdit"
      :group-id="queryForm.groupId"
      :role="20"
      :corp-id="queryForm.corpId"
      @send-close="sendClose"
    />
    <add-contract :dialog-add-contract="dialogAddContract" :worker-info="curWorkerInfo" @send-close="sendClose" />
    <detail v-if="dialogDetail.visible" :dialog-detail="dialogDetail" @send-close="sendClose" />
    <!--转入管理组-->
    <el-dialog v-model="dialogManager.visible" width="650px" title="转入管理组" close-icon="CloseBold">
      <el-form ref="managerFormRef" :model="formManagerData" :rules="rules">
        <el-row justify="start" :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="formManagerData.name" placeholder="请输入姓名" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="mobile">
              <el-input v-model="formManagerData.mobile" placeholder="请输入联系电话" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参建单位" prop="corpId">
              <el-select v-model="formManagerData.corpId" class="last-select" placeholder="请选择" clearable>
                <el-option v-for="item in projectCorps" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属岗位" prop="postCategory">
              <el-select v-model="formManagerData.postCategory" class="last-select" placeholder="请选择" clearable>
                <el-option
                  v-for="item in projectGropuMemberManageType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进场日期" prop="startDate">
              <el-date-picker
                v-model="formManagerData.startDate"
                type="date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退场日期" prop="endDate">
              <el-date-picker
                v-model="formManagerData.endDate"
                type="date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="工资开卡银行" prop="bankName">
              <el-input v-model="formManagerData.bankName"  placeholder="请输入工资开卡银行" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工资卡卡号" prop="cardNo">
              <el-input v-model="formManagerData.cardNo"  placeholder="请输入工资卡卡号" />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog_footer">
          <el-button type="primary" :loading="Managerloading" @click="saveManager">确定 </el-button>
          <el-button @click="closeDialogManager()">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import addForm from "./addForm.vue";
import addContract from "./addContract.vue";
import detail from "./detail.vue";
import { getProjectCorps, QueryWorkerList, EditTransferManager } from "@/api/worker/index";
import { getDicts } from "@/store/modules/dict";

//声明变量
const projectCorps = ref<any[]>([]);
/**权限处理 */
const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor")); //是否是总包
const projectGropuMemberManageType = getDicts("projectGropuMemberManageType");
const permissions = ref({
  create: true,
  update: true,
  detail: true,
  transferManager: true,
});
if (currentSystem.value == "Constructor" || currentSystem.value == "Commander") {
  permissions.value = {
    create: false,
    update: false,
    detail: true,
    transferManager: false,
  };
}
const queryForm = reactive({
  pageIndex: 1,
  pageSize: 10,
  keywords: "",
  groupId: "",
  corpId: "",
} as any);
const total = ref(0);

const dialogSelectProject = reactive({
  visible: false,
  title: "选择项目",
  footBtn: "确认",
  id: 0,
  type: "allproject",
});

//获取参建单位
const loadprojectCorps = () => {
  if (currentSystem.value == "SysUser") {
    console.log(formManagerData.value);
    getProjectCorps(formManagerData.value.projectId).then((res) => {
      if (res.type == 0) {
        if (res.success) {
          projectCorps.value = res.data;
        } else {
        }
      } else {
      }
    });
    return;
  }
  getProjectCorps().then((res) => {
    if (res.type == 0) {
      if (res.success) {
        projectCorps.value = res.data;
      } else {
      }
    } else {
    }
  });
};

//表格
const loading = ref(false);
const tableData = ref();
const formManagerData = ref<any>();
const curWorkerInfo = ref();

const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
});
const dialogAddContract = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
});
const selectedRows = ref([] as any[]);
const tableRef = ref();
const dialogManager = reactive<DialogOption>({
  visible: false,
});
//实现复选框的单选
const handleCurrentChange = (selections: any, row: any) => {
  selectedRows.value = selections;
};

//新增
const Create = () => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "确认新增";
  dialogAddOrEdit.title = "新增";
  dialogAddOrEdit.type = 1;
};
//编辑
const Edit = (row: any) => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.id = row.id;
  dialogAddOrEdit.footBtn = "确认修改";
  dialogAddOrEdit.title = "修改";
  dialogAddOrEdit.type = 2;
};
//转入管理组
const TransferManager = (row: any) => {
  dialogManager.visible = true;
  dialogManager.id = row.id;
  dialogManager.footBtn = "确认";
  formManagerData.value = row;
  loadprojectCorps();
};

//详情
const handleDetail = (row: any) => {
  dialogDetail.visible = true;
  dialogDetail.id = row.id;
  dialogDetail.title = "详情";
};

const sendClose = (needRefresh: boolean, workerInfo?: any) => {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  if (needRefresh) {
    handleQuery();
  }
  if (workerInfo) {
    dialogAddContract.visible = true;
    dialogAddContract.title = "添加合同";
    dialogAddContract.footBtn = "确认添加";
    curWorkerInfo.value = workerInfo;
  }
};

import { FormRules } from "element-plus/es/components/form/src/types";
const rules = reactive<FormRules<any>>({
  corpId: [
    {
      required: true,
      message: "请选择参建单位",
      trigger: "change",
    },
  ],
  postCategory: [
    {
      required: true,
      message: "请选择岗位类型",
      trigger: "change",
    },
  ],
  startDate: [
    {
      required: true,
      message: "请选择进场日期",
      trigger: "change",
    },
  ],
});

function closeDialogManager() {
  dialogManager.visible = false;
}

const Managerloading = ref(false);
const managerFormRef = ref(ElForm);
function saveManager() {
  managerFormRef.value.validate((valid: any) => {
    if (valid) {
      ElMessageBox.confirm("确定转入管理组吗？", "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",

        type: "warning",
      })
        .then(() => {
          Managerloading.value = true;
          EditTransferManager(formManagerData.value)
            .then((res) => {
              if (res.type === 0) {
                if (res.success) {
                  ElMessage({
                    type: "success",
                    message: res.msg,
                  });
                  handleQuery();
                  Managerloading.value = false;
                  dialogManager.visible = false;
                } else {
                  ElMessage({
                    type: "error",
                    message: res.msg,
                  });
                }
              }
            })
            .finally(() => {
              Managerloading.value = false;
            });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消！",
          });
        });
    }
  });
}
//查询条件

//这是列表数据
const handleQuery = function () {
  loading.value = true;
  QueryWorkerList(queryForm)
    .then((res) => {
      if (res.type === 0) {
        if (res.success) {
          tableData.value = res.data.list;
          queryForm.pageIndex = res.data.pageNum;
          queryForm.pageSize = res.data.pageSize;
          total.value = res.data.total;
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>
<style scoped>
.table-container {
  padding: 15px;
  min-height: 680px;
}
:deep(.el-select) {
  width: 100%;
}

:deep(.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content) {
  background-color: #3b98f7 !important;
  color: #fff;
  fill: #fff !important;
}

:deep(.el-tree-node__content) {
  color: #000;
}

:deep(.el-button) {
  margin-left: 5px;
}

:deep(.el-input__wrapper) {
  border-radius: 0;
}
</style>
